package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDefaultDataBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressEditDataBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/4/13.
 */

public class OrderAddressEditModel implements OrderAddressEditModelImpl{

    private OrderAddressEditModel.OrderUserAddressEditDataResponseListener mListener;

    private static final String TAG = "OrderAddressEditModel";

    public OrderAddressEditModel(OrderAddressEditModel.OrderUserAddressEditDataResponseListener listener) {
        mListener = listener;
    }
    public interface OrderUserAddressEditDataResponseListener{
        void onGetDetailSuccess(OrderUserAddressEditDataBean bean);
        void onGetDetailError(Throwable throwable);

    }

    /**
     * 添加或修改地址网络请求
     * @param user_id  用户id
     * @param truename 用户名
     * @param telephone 电话
     * @param province 省
     * @param city 市
     * @param county 区
     * @param address 具体地址
     * @param label 标签
     * @param def 默认
     */
    @Override
    public void getUserAddressAdd(String user_id, String truename, String telephone, String province, String city,
                                        String county, String address, String label, String def) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("user_id", user_id)
                .addFormDataPart("truename",truename)
                .addFormDataPart("telephone",telephone)
                .addFormDataPart("province",province)
                .addFormDataPart("city",city)
                .addFormDataPart("county",county)
                .addFormDataPart("address",address)
                .addFormDataPart("label",label)
                .addFormDataPart("def",def)
                .build();
        Observable<OrderUserAddressEditDataBean> observable = DataManager.getOrderUserAddressEditData(body);
        Observable<OrderUserAddressEditDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderUserAddressEditDataBean>() {
            @Override
            public void onNext(OrderUserAddressEditDataBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getUserAddressChange(String user_id, String address_id, String truename, String telephone,
                                     String province, String city, String county, String address, String label,
                                     String def) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("user_id", user_id)
                .addFormDataPart("address_id",address_id)
                .addFormDataPart("truename",truename)
                .addFormDataPart("telephone",telephone)
                .addFormDataPart("province",province)
                .addFormDataPart("city",city)
                .addFormDataPart("county",county)
                .addFormDataPart("address",address)
                .addFormDataPart("label",label)
                .addFormDataPart("def",def)
                .build();
        Observable<OrderUserAddressEditDataBean> observable = DataManager.getOrderUserAddressEditData(body);
        Observable<OrderUserAddressEditDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderUserAddressEditDataBean>() {
            @Override
            public void onNext(OrderUserAddressEditDataBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }

}
